﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour {

    public Transform boom_BagPos_Screen;
    public Transform potion_BagPos_Screen;
    public Transform god_BagPos_Screen;
    public Transform timeback_BagPos_Screen;


    public GameObject Bags;
    public GameObject Time;
    public Sprite redHeart;
    public Sprite whiteHeart;
    public GameObject Life;
    private bool IsFaileedPanelOpen = false;
    private bool IsWinedPanelOpen = false;
    private Text _time;

    public  Player player;
    private static UIManager _instance;
    public static UIManager Instance
    {
        get
        {
            return _instance;
        }
    }
    // Use this for initialization
    void Start () {
        _instance = this;
        _time = transform.Find("Time").GetComponent<Text>();
        Bags = transform.Find("Bag").gameObject;
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    public void Initalize()
    {
        if(IsFaileedPanelOpen==true)
        {
            CloseFailedPanel();
        }
        if (IsWinedPanelOpen == true)
        {
            CloseFailedPanel();
        }
        //
        //道具数量清零
        ClearPropsCount();
        foreach(Transform child in Life.transform)
        {
            child.transform.GetComponent<Image>().sprite = redHeart;
        }
        player.Initalize();
       player.IsPlayerAlive= true;
    } 

    public void OnRestartBtnDown()
    {
        Debug.Log("restart");
        // gameObject.BroadcastMessage("Initalize", SendMessageOptions.DontRequireReceiver);
        Application.LoadLevel("Main");


    }
    /// <summary>
    /// 改变道具的数量
    /// </summary>
    /// <param name="PropName"></param>
    /// <param name="Count"></param>
    public void ChangeProps(string PropName,int Count)
    {
        GameObject prop= Bags.transform.Find(PropName + "/Count/countText").gameObject;
        prop.GetComponent<Text>().text =( int.Parse(prop.GetComponent<Text>().text) + Count).ToString();

    }

    public int GetPropsCount(string PropName)
    {
        GameObject prop = Bags.transform.Find(PropName + "/Count/countText").gameObject;
        return int.Parse( prop.GetComponent<Text>().text);
    }

    private void ClearPropsCount()
    {
        foreach(Transform child in Bags.transform)
        {
            child.Find("Count/countText").GetComponent<Text>().text =  "0";
        }
    }

    public void ChangeLifeSprite(int lifecount)
    {
        switch (lifecount)
        {
            case 3:
                Life.transform.GetChild(2).GetComponent<Image>().sprite = redHeart;
                Life.transform.GetChild(1).GetComponent<Image>().sprite = redHeart;
                Life.transform.GetChild(0).GetComponent<Image>().sprite = redHeart;
                break;
            case 2:
                Life.transform.GetChild(2).GetComponent<Image>().sprite = whiteHeart;
                Life.transform.GetChild(1).GetComponent<Image>().sprite = redHeart;
                Life.transform.GetChild(0).GetComponent<Image>().sprite = redHeart;
                break;
            case 1:
                Life.transform.GetChild(2).GetComponent<Image>().sprite = whiteHeart;
                Life.transform.GetChild(1).GetComponent<Image>().sprite = whiteHeart;
                Life.transform.GetChild(0).GetComponent<Image>().sprite = redHeart;
                break;
            case 0:
                Life.transform.GetChild(2).GetComponent<Image>().sprite = whiteHeart;
                Life.transform.GetChild(1).GetComponent<Image>().sprite = whiteHeart;
                Life.transform.GetChild(0).GetComponent<Image>().sprite = whiteHeart;
                break;
            default:
                break;
        }
    }

    public void ShowFailedPanel()
    {
        transform.Find("FailedPanel").gameObject.SetActive(true);
        IsFaileedPanelOpen = true;
    }

    public void ShowWinPanel()
    {
        transform.Find("WonPanel").gameObject.SetActive(true);
        IsWinedPanelOpen = true;
    }
    public void CloseFailedPanel()
    {
        transform.Find("FailedPanel").gameObject.SetActive(false);
        transform.Find("WonPanel").gameObject.SetActive(false);
        IsFaileedPanelOpen = false;
        IsWinedPanelOpen = false;
    }

   
}
